package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcFollowDataBpressureMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcFollowDataBoxygen;
import com.csun.cmny.provider.model.domain.DmcFollowDataBpressure;
import com.csun.cmny.provider.model.dto.follow.FollowDataPageQuery;
import com.csun.cmny.provider.model.dto.follow.UpFollowDataBpressureDto;
import com.csun.cmny.provider.model.enums.follow.FollowBoxygenStatusEnum;
import com.csun.cmny.provider.model.enums.follow.FollowBpressureStatusEnum;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.service.DmcFollowDataBpressureService;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-16 21:43
 * @Description: the class dmc follow data bpressure service impl
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcFollowDataBpressureServiceImpl extends BaseService<DmcFollowDataBpressure>
        implements DmcFollowDataBpressureService {

    @Resource
    private DmcDeviceService dmcDeviceService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private DmcFollowDataBpressureMapper followDataBpressureDao;

    /**
     * query dmc follow data bpressure list with page
     *
     * @param followDataPageQuery follow data query page
     *
     * @return the page info
     */
    @Override
    public PageInfo queryDmcFollowDataBpressureListWithPage(FollowDataPageQuery followDataPageQuery) {

        PageHelper.startPage(followDataPageQuery.getPageNum(), followDataPageQuery.getPageSize());

        List<DmcFollowDataBpressure> dmcFollowDataBpressureList = followDataBpressureDao
                .queryDmcFollowDataBpressureListWithPage(followDataPageQuery);

        return new PageInfo<>(dmcFollowDataBpressureList);
    }

    /**
     * up follow data bpressure
     *
     * @param upFollowDataBpressureDto up follow data bpressure dto
     * @param loginAuthDto             login auth dto
     *
     * @return the int.
     */
    @Override
    public Integer upFollowDataBpressure(UpFollowDataBpressureDto upFollowDataBpressureDto,
                                         LoginAuthDto loginAuthDto) {
        // 1.校验设备
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsFollowByDeviceId(upFollowDataBpressureDto.getDeviceId());
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610041, upFollowDataBpressureDto.getDeviceId());
        }

        // 2.校验参数
        Preconditions.checkArgument(!PubUtils.isNull(upFollowDataBpressureDto.getSbp()),
                cmnyMessageSource.getMessage("follow.up.bpressure.sbp.null", null));

        Preconditions.checkArgument(!PubUtils.isNull(upFollowDataBpressureDto.getDbp()),
                cmnyMessageSource.getMessage("follow.up.bpressure.dbp.null", null));

        // 3.换算状态
        String status = FollowBpressureStatusEnum.getStatus(upFollowDataBpressureDto.getSbp(),
                upFollowDataBpressureDto.getDbp());
        if (PubUtils.isNull(status)) {
            status = cmnyMessageSource.getMessage("follow.bpressure.status.error", null);
        }

        // 4.插入数据
        DmcFollowDataBpressure dmcFollowDataBpressure = new DmcFollowDataBpressure();
        BeanUtils.copyProperties(upFollowDataBpressureDto, dmcFollowDataBpressure);
        dmcFollowDataBpressure.setStatus(status);
        dmcFollowDataBpressure.setUpdateInfo(loginAuthDto);

        return followDataBpressureDao.insertSelective(dmcFollowDataBpressure);
    }

    @Override
    public Map<String,Object> getRecentData(Integer oldmanId){
        return followDataBpressureDao.getRecentData(oldmanId);
    }

    @Override
    public List<Map<String, Object>> getAllOldmanData(Integer oldmanId) {
        return followDataBpressureDao.getAllOldmanData(oldmanId);
    }
}
